/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.hlingsoft.kkh.action.admin;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.PutObjectResult;
import com.hlingsoft.kkh.dao.AdminResumeDao;
import com.hlingsoft.kkh.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传
 */
@Controller
public class UpLoadAction {

    @Autowired
    UserDao userDao;
    @Autowired
    AdminResumeDao adminResumeDao;




    /**
     * 个人资料头像上传
     *
     * @param file
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/events/photo.json", method = RequestMethod.POST)
    public Map<String, Object> userPhoto(
            @RequestParam(value = "userId") int userId,
            @RequestParam(value = "file") MultipartFile file,
                                         HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try{
                String fileName = file.getOriginalFilename();
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = simpleFormat.format(new Date());
                putObject(dateString, fileName, file.getInputStream(),
                        file.getSize());
                String headImage="http://dlg.oss-cn-hangzhou.aliyuncs.com/upload/"+ dateString + "/" + fileName;
//                userDao.updateHeadImage(userId, headImage);
                resultMap.put("path", headImage);
                resultMap.put("result", true);

        } catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }

    /**
     * 简历头像上传
     *
     * @param file
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/resume/photo.json", method = RequestMethod.POST)
    public Map<String, Object> resumePhoto(
            @RequestParam(value = "userId") int userId,
            @RequestParam(value = "file") MultipartFile file,
                                           HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

                String fileName = file.getOriginalFilename();
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = simpleFormat.format(new Date());
                putObject(dateString, fileName, file.getInputStream(),
                        file.getSize());
                String headImage="http://dlg.oss-cn-hangzhou.aliyuncs.com/upload/"
                        + dateString + "/" + fileName;
//                adminResumeDao.updateResumeHeadImage(userId,headImage);
                resultMap.put("path", headImage);
                resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

    public boolean checkPhotoFile(MultipartFile file) {
        if (file.isEmpty()) {
            return false;
        }
        if (file.getContentType().equals("image/jpg")
                || file.getContentType().equals("image/png")
                || file.getContentType().equals("image/jpeg")
                || file.getContentType().equals("image/gif")) {
            return true;
        } else {
            return false;
        }
    }

    public void putObject(String date, String filename, InputStream content,
                          Long size) throws FileNotFoundException {

        // 初始化OSSClient
        OSSClient client = new OSSClient("W6PVM427mSEWlvEz",
                "h7NoXlxcjCqyeOWeuT43LungGmGRSd");


        // 获取指定文件的输入流
        // File file = new File(filePath);
        // InputStream content = new FileInputStream(file);

        // 创建上传Object的Metadata
        ObjectMetadata meta = new ObjectMetadata();

        // 必须设置ContentLength
        meta.setContentLength(size);

        // 上传Object.
        PutObjectResult result = client.putObject("dlg", "upload/" + date
                + "/" + filename, content, meta);

        // 打印ETag
        System.out.println(result.getETag());
    }

}
